<?php

namespace App\Controllers;

class Test extends BaseController
{
    public function email(): \CodeIgniter\HTTP\ResponseInterface
    {
        $email = \Config\Services::email();

        $rules = [
            "to"      => 'required|valid_email',
            "title"   => 'required|string|min_length[2]|max_length[200]',
            "content" => 'required|string',
        ];

        if (!$this->validate($rules)) {
            $errors = $this->validator->getErrors();

            return $this->responseMsg(array_shift($errors));
        }

        $post = $this->validator->getValidated();

        $email->setFrom('chafang0131@163.com', '山猫');
        $email->setTo($post['to']);

        $email->setSubject($post['title']);
        $email->setMessage($post['content']);

        if ($email->send()) {
            return $this->responseJson();
        } else {
            return $this->responseJson("发送失败");
        }
    }

    public function info()
    {
        phpinfo();
    }
}